package com.itheima.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.ums.UmsResourceMapper;
import com.itheima.pojo.ums.UmsResource;
import com.itheima.service.ums.UmsResourceService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements UmsResourceService {
    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @Override
    public void create(UmsResource umsResource) {
        umsResource.setCreateTime(LocalDateTime.now());
        this.save(umsResource);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @Override
    public void delete(Integer id) {
        this.removeById(id);
    }

    /**
     * 查询所有后台资源
     *
     * @return
     */
    @Override
    public List<UmsResource> listAll() {
        return this.list();
    }

    /**
     * 修改后台资源
     *
     * @param umsResource
     * @return
     */
    @Override
    public boolean updateResource(UmsResource umsResource) {
        return this.updateById(umsResource);
    }


    /**
     * 根据ID获取资源详情
     *
     * @param id
     * @return
     */
    @Override
    public UmsResource selectById(Integer id) {
        UmsResource umsResource = this.getById(id);
        return umsResource;
    }

    /**
     * 分页模糊查询后台资源
     *
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo likeAll( Integer categoryId, String nameKeyword, String urlKeyword, Integer pageNum, Integer pageSize) {
        Page<UmsResource> umsResourcePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null, UmsResource::getCategoryId, categoryId)
                .like(nameKeyword != null, UmsResource::getName, nameKeyword)
                .like(urlKeyword != null, UmsResource::getUrl, urlKeyword)
                .orderByAsc(UmsResource::getCreateTime);
        Page<UmsResource> page = this.page(umsResourcePage, queryWrapper);
        //封装返回数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(page.getRecords());
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalPage((int) page.getCurrent());
        return pageInfo;
    }
}
